//
//  StringExtensions.swift
//
//
//  Created by Devran on 19.09.19.
//
import RegexBuilder

extension String {
    internal func chunked(by groupCount: Int) -> [String] {
        let startIndex = self.startIndex
        return (0..<(self.count / groupCount)).map { (index: Int) -> String in
            let offset = index * groupCount
            let subStringStartIndex = self.index(startIndex, offsetBy: offset)
            let subStringEndIndex = self.index(startIndex, offsetBy: offset + groupCount)
            return String(self[subStringStartIndex..<subStringEndIndex])
        }
    }
    
    func extractDigit() -> String{
        let pattern = "(\\d+)"
        do {
            // 创建NSRegularExpression实例
            let regex = try NSRegularExpression(pattern: pattern)
            // 执行匹配（范围：整个字符串）
            let matches = regex.matches(in: self, range: NSRange(startIndex..., in: self))
            
            return matches.map { r in
                self[Range(r.range, in: self)!]
            }.joined()
        } catch {
            debugPrint("\(error)")
            return ""
        }
    }
    
}
